package com.kgc.itrip.controller;

import com.kgc.itrip.common.Constants;
import com.kgc.itrip.common.DateUtil;
import com.kgc.itrip.common.DtoUtil;
import com.kgc.itrip.common.EmptyUtils;
import com.kgc.itrip.dto.Dto;
import com.kgc.itrip.pojo.ItripHotelRoom;
import com.kgc.itrip.pojo.ItripImage;
import com.kgc.itrip.pojo.ItripLabelDic;
import com.kgc.itrip.response.ResultCode;
import com.kgc.itrip.service.ItripHotelRoomService;
import com.kgc.itrip.service.ItripImageService;
import com.kgc.itrip.service.ItripLabelDicService;
import com.kgc.itrip.vo.ItripImageVO;
import com.kgc.itrip.vo.ItripLabelDicVO;
import com.kgc.itrip.vo.hotelroom.ItripHotelRoomVO;
import com.kgc.itrip.vo.hotelroom.SearchHotelRoomVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**酒店房间的控制器*/
@Api(value = "酒店房间的控制器")
@Slf4j
@RestController
@RequestMapping("/api/hotelroom")
public class HotelRoomController {
    @Resource
    private ItripHotelRoomService itripHotelRoomService;
    @Resource
    private ItripImageService itripImageService;
    @Resource
    private ItripLabelDicService itripLabelDicService;

    @ApiOperation(value = "查询酒店床型列表",
            httpMethod = "GET",
            notes = " success = ‘true’ | 失败：success = ‘false’ 并返回错误码，" +
                    "如下 ：100305 : 获取酒店房间床型失败",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application/json")
    @GetMapping(value= "/queryhotelroombed")
    public Dto<List<ItripLabelDicVO>> queryHotelRoomBed() {

        try {
            Map<String, Object> params = new HashMap<>();
            params.put("parentId", 1);
            List<ItripLabelDic> itripLabelDicList = itripLabelDicService.getItripLabelDicListByMap(params);
            List<ItripLabelDicVO> labelDicVOList = null;
            if (EmptyUtils.isNotEmpty(itripLabelDicList)) {
                labelDicVOList = new ArrayList<>();
                ItripLabelDicVO labelDicVO = null;
                for (ItripLabelDic itripLabelDic : itripLabelDicList) {
                    labelDicVO = new ItripLabelDicVO();
                    BeanUtils.copyProperties(itripLabelDic, labelDicVO);
                    labelDicVOList.add(labelDicVO);
                }
            }

            return DtoUtil.returnDataSuccess(labelDicVOList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取酒店房间床型失败");
            return DtoUtil.returnFail(ResultCode.HOTEL_FANG_NULL.getMsg(), ResultCode.HOTEL_FANG_NULL.getCode());
        }

    }


    @ApiOperation(value = "根据酒店房型id查询酒店房型图片",
            httpMethod = "GET",
            notes = " success = ‘true’ | 失败：success = ‘false’ 并返回错误码，" +
            "如下：100301 : 获取酒店房型图片失败:100302 : 酒店房型id不能为空",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application/json")
    @GetMapping(value= "/getimg/{targetId}")
    public Dto<List<ItripImageVO>> getImg(@ApiParam(value = "酒店id",required = true,name = "targetId")
            @PathVariable String targetId){
        if(EmptyUtils.isEmpty(targetId)){
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_NULL_FOR.getMsg(),ResultCode.HOTEL_IS_NULL_FOR.getCode());
        }
        Map<String,Object> params = new HashMap<>();
        params.put("type", Constants.IMAGE_TYPE.ROOM_IMAGE);
        params.put("targetId",targetId);

        try{
            //调用业务逻辑层
            List<ItripImage> itripImageList = itripImageService.getItripImageListByMap(params);
            //创建放回的图片集合
            List<ItripImageVO> imageVOList = new ArrayList<>();
            for (ItripImage itripImage : itripImageList) {
                ItripImageVO imageVO = new ItripImageVO();
                BeanUtils.copyProperties(itripImage,imageVO);
                imageVOList.add(imageVO);
            }
            return DtoUtil.returnDataSuccess(imageVOList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询酒店房型图片失败");
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_NULL_FOR.getMsg(),ResultCode.HOTEL_IS_NULL_FOR.getCode());
        }

    }

    @ApiOperation(value = "查询酒店房间列表",
            httpMethod = "POST",notes = " success = ‘true’ | 失败：success = ‘false’ 并返回错误码，" +
            "如下：100303 : 酒店id不能为空,酒店入住及退房时间不能为空,入住时间不能大于退房时间" +
            "100304 : 系统异常",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application/json")
    @PostMapping(value= "/queryhotelroombyhotel")
    public Dto<List<List<ItripHotelRoomVO>>> queryHotelRoomByHotel(@RequestBody SearchHotelRoomVO vo){

        if(vo.getHotelId()==null||vo.getHotelId()==0){
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_NULL_TAIN.getMsg(),ResultCode.HOTEL_IS_NULL_TAIN.getCode());
        }
        if(vo.getStartDate()==null||vo.getEndDate()==null){
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_NULL_TAIN.getMsg(),ResultCode.HOTEL_IS_NULL_TAIN.getCode());
        }
        if(vo.getStartDate().getTime() > vo.getEndDate().getTime()){
            return DtoUtil.returnFail(ResultCode.HOTEL_IS_NULL_TAIN.getMsg(),ResultCode.HOTEL_IS_NULL_TAIN.getCode());
        }
        Map<String,Object> params = new HashMap<>();
        params.put("hotelId",vo.getHotelId());
        params.put("roomBeTypeId",vo.getRoomBedTypeId());
        params.put("isHavingBreakfast",vo.getIsHavingBreakfast());
        params.put("isBook",vo.getIsBook());
        params.put("isCancel",vo.getIsCancel());
        params.put("isTimelyResponse",vo.getIsTimelyResponse());
        params.put("payType",(null==vo.getPayType() || vo.getPayType()==0 || vo.getPayType()==3) ? null : vo.getPayType());
        params.put("dateList", DateUtil.getBetweenDates(vo.getStartDate(),vo.getEndDate()));
        try{
            List<ItripHotelRoom> itripHotelRoomsList = itripHotelRoomService.getItripHotelRoomListByMap(params);
            List<List<ItripHotelRoomVO>> roomVOList = null;
            if(EmptyUtils.isNotEmpty(itripHotelRoomsList)){
                roomVOList =new ArrayList<>();
                List<ItripHotelRoomVO> tempRoomVOList = null;
                ItripHotelRoomVO roomVO = null;
                for (ItripHotelRoom itripHotelRoom : itripHotelRoomsList) {
                    tempRoomVOList = new ArrayList<>();
                    roomVO = new ItripHotelRoomVO();
                    BeanUtils.copyProperties(itripHotelRoom,roomVO);
                    roomVO.setRoomPrice(BigDecimal.valueOf(itripHotelRoom.getRoomPrice()));
                    tempRoomVOList.add(roomVO);
                    roomVOList.add(tempRoomVOList);
                }
            }
            return DtoUtil.returnDataSuccess(roomVOList);

        }catch (Exception e){
            e.printStackTrace();
            log.error("系统异常：查询酒店房间列表失败");
            return DtoUtil.returnFail(ResultCode.SYSTEM_IS_ERROR.getMsg(),ResultCode.SYSTEM_IS_ERROR.getCode());
        }
    }
}
